/******************************************************************************
 * SAGE - Scalable Adaptive Graphics Environment
 *
 * Module: sageStreamer.h
 * Author : Byungil Jeong
 *
 * Copyright (C) 2004 Electronic Visualization Laboratory,
 * University of Illinois at Chicago
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either Version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser Public License along
 * with this library; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * Direct questions, comments etc about SAGE to bijeong@evl.uic.edu or 
 * http://www.evl.uic.edu/cavern/forum/
 *
 *****************************************************************************/

#ifndef SAGESTREAMER_H_
#define SAGESTREAMER_H_

#include "sage.h"
#include "sageSync.h"
#include "streamProtocol.h"
#include "sageTcpModule.h"
#include "sageUdpModule.h"
#include "streamInfo.h"
#include <deque>

typedef struct {
	int  rcvID;
	int  tileID;
	bool active;
	streamInfo *sInfo;
} streamParam;

class streamerConfig {
public:
	bool master;
	nwProtocol protocol;
	int nodeNum, nodeID;
	int fsIdx;
	int syncPort;
	char *masterIP;
	sageNwConfig nwCfg;
	int bufferMode;
	
	streamerConfig() : master(false), protocol(SAGE_TCP), nodeNum(1), fsIdx(0), 
			syncPort(0), masterIP(NULL) {}
};

class sageCircBuf;

class sageStreamer {
private:
	int winID;
	streamProtocol *nwObj;
	
	int totalConnectionNum;
	int senderNum;
	int rcvNum;
   int streamNum;
	int tileNum;
	bool streamerOn;
	bool perfOn;
	int bufferMode;
	
	streamParam *params;
	streamGroup sGrp;
	sageChildBlock *childBlocks;
	int childBlockNum;
	
	sageRect imageMap; 
   int margin;
	int bytesPerPixel;
	bool reconfig;
	bool updateChild;
		
	int streamPort;
	int nwID;
	int syncMode;
	bool rcvsReady;
	bool timeBlockFlag;
	unsigned long totalBandWidth;
	int masterFrameNum;
	
	bool pauseSync, master;
	nwProtocol protocol;
	int nodeNum, nodeID, fsIdx;
	sageSyncServer *syncServerObj;	
	sageSyncClient *syncClientObj;
	pthread_mutex_t *streamMutex;
	sageNwConfig    nwCfg;
	sageBlock initBlock;
	std::deque<char *> syncMsgQueue;
	
	sageCircBuf *blockBuf;
	static void* nwThread(void *args);
	
	int streamInitBlock(char *streamData);
	int streamSageBlock(sageBlock &block);
	int sendTimeBlock(int frame);
   
public:
   sageStreamer();
   ~sageStreamer();
	int init(streamerConfig &config);
	int initStreams(char *data);
	int connectToRcv(sageToken &tokenBuf);
	int initNetworks(char *data);
	int checkSyncServer();
	
	friend class appInstance;
	friend class sail;
};

#endif
